package MapReduce.writableBean;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * @Author lixinlei
 * @Date 2023/3/23 8:31
 */
public class ProductAndOrderWritable implements WritableComparable<ProductAndOrderWritable> {

    private String orderId = "";
    private String productId = "";
    private int count;

    private String productName = "";
    private String flag = "";


    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    public String getProductId() {
        return productId;
    }

    public void setProductId(String productId) {
        this.productId = productId;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public String getFlag() {
        return flag;
    }

    public void setFlag(String flag) {
        this.flag = flag;
    }

    @Override
    public int compareTo(ProductAndOrderWritable o) {

        String thisProductId = this.productId;
        String thatProductId = o.getProductId();

        String thisOrderId = this.orderId;
        String thatOderId = o.getOrderId();

        if (thisProductId.equals(thatProductId)) {
            return thisOrderId.compareTo(thatOderId);
        }else{
            return thisProductId.compareTo(thatProductId);
        }
    }

    @Override
    public void write(DataOutput out) throws IOException {
        out.writeUTF(orderId);
        out.writeUTF(productId);
        out.writeInt(count);
        out.writeUTF(productName);
        out.writeUTF(flag);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        this.orderId = in.readUTF();
        this.productId = in.readUTF();
        this.count = in.readInt();
        this.productName = in.readUTF();
        this.flag = in.readUTF();
    }

    @Override
    public String toString() {
        return orderId+"\t"+productId+"\t"+productName+"\t"+count;
    }
}
